package mq_client

import (
	"context"
	"encoding/json"
	"github.com/zeromicro/go-queue/kq"
	"im_sys/apps/task/mq/mq_struct"
)

type MqClient interface {
	PushMsgToKafka(msg *mq_struct.ChatMsg) error
}

type mqClient struct {
	pusher *kq.Pusher
}

func NewMsgMqClient(brokers []string, topic string, opt ...kq.PushOption) MqClient {
	return &mqClient{
		pusher: kq.NewPusher(brokers, topic, opt...),
	}
}

// PushMsgToKafka 发送方发送消息会触发该方法，将消息推入kafka
func (c *mqClient) PushMsgToKafka(msg *mq_struct.ChatMsg) error {
	data, err := json.Marshal(msg)
	if err != nil {
		return err
	}

	return c.pusher.Push(context.Background(), string(data))
}
